package BookManage;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import BookDB.*;
import Loginout.UserBean;

@WebServlet("/Manage")
public class Manage extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		UserBean UB = (UserBean) session.getAttribute("ub");

		if (UB == null) {
			resp.sendRedirect("error.html");
		} else if (UB.vaild()) {
			try {
				BookBean BB = null;
				String isbn = req.getParameter("isbn");

				BB = h2DAO.SelectBook(isbn);
				if (BB.getIsbn()=="" ||BB==null) {
					PrintWriter writer = resp.getWriter();
					writer.print("No this book here!! " + "ISBN No:"+isbn);
					
				}
				else
				{
				if (BB.isStatus()) {
					req.setAttribute("bb", BB);
					RequestDispatcher rd;
					rd = req.getRequestDispatcher("/BookReturn.jsp");
					rd.forward(req, resp);
				} else {
					req.setAttribute("bb", BB);
					RequestDispatcher rd;
					rd = req.getRequestDispatcher("/BookBorrow.jsp");
					rd.forward(req, resp);
				}
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			resp.sendRedirect("login.html");
		}
	}
}
